package com.snc.controller.app;


import com.snc.controller.index.BaseController;
import com.snc.entity.CommentFormMap;
import com.snc.entity.ResidentFormMap;
import com.snc.entity.StuTeacherFormMap;
import com.snc.exception.BusinessException;
import com.snc.plugin.Result;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * APP-活动
 */
@Controller
@RequestMapping("/app/st")
public class StuTeacherController extends BaseController {

    /**
     * 获取活动的列表，
     * 请求参数：
     *
     * @return 数据
     */
    @ResponseBody
    @RequestMapping(value = "/list")
    public Result list() {
        StuTeacherFormMap map = super.toFormMap(StuTeacherFormMap.class);
        return Result.Success(StuTeacherFormMap.mapper().select(map));
    }

    @ResponseBody
    @RequestMapping("jxInfo")
    public Result jxInfo(Long stuId){
        StuTeacherFormMap map = super.toFormMap(StuTeacherFormMap.class);
        List<StuTeacherFormMap> list = map.findByWhere();
        if(org.apache.commons.collections4.CollectionUtils.isNotEmpty(list)){
            Long teacherId = list.get(0).getLong("teacherId");
            ResidentFormMap residentFormMap = new ResidentFormMap();
           List<ResidentFormMap> list1=  residentFormMap.findByAttribute("id",teacherId);
            return Result.Success(list1.get(0));
        }
        return Result.Success(null);
    }

    @ResponseBody
    @RequestMapping("/add")
    public Result add() throws Exception {
        StuTeacherFormMap map = super.toFormMap(StuTeacherFormMap.class);
        map.put("where"," stuId ="+map.getStr("stuId")+" and teacherId="+map.getStr("teacherId"));
        List<StuTeacherFormMap> list = map.findByWhere();
        if (CollectionUtils.isNotEmpty(list)) {
            throw new BusinessException("该学员已添加");
        }
        map.save();
        return Result.Success(null);
    }

    @ResponseBody
    @RequestMapping("/del")
    public Result del( ) throws Exception {
        StuTeacherFormMap map = super.toFormMap(StuTeacherFormMap.class);
        List<StuTeacherFormMap> list = map.findByWhere();
        for (StuTeacherFormMap st : list) {
            map.deleteById(st.get("id") + "");
        }
        return Result.Success(null);
    }

}